/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.services;

import java.util.Vector;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;

/**
 *
 * @author hjhi
 */
@Service("shortestpathservice")
public class ShortestPathService {

    ApplicationContext ac = new ClassPathXmlApplicationContext("//applicationContext.xml");
    DataSource dataSource = (DataSource) ac.getBean("dataSource");
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

    public Vector shortestPath() {
        Vector gids=new Vector();
        String query = "SELECT * FROM shortest_path('SELECT gid AS id,start_id::int4 AS source,end_id::int4 AS target,shape_leng::float8 AS cost FROM network',?,?,false,false)";        
        Object[] parameters = new Object[]{1, 50};
        SqlRowSet c = jdbcTemplate.queryForRowSet(query, parameters);
//        gids.add(c.);
        return gids;
    }
}    
    
    
    
    

//    public Object findShortPath() {
//LoginForm loginForm = (LoginForm)jdbcTemplate.queryForObject(
//    "select first_name, surname from t_actor where id = ?;",
//    new Object[]{new Integer(1)},
//    new RowMapper() {
//
//        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
//            LoginForm lf = new LoginForm();
//            lf.setUserName(rs.getString("username"));
//            lf.setPassword(rs.getString("password"));
//            return lf;
//        }
//
//            @Override
//            public int[] getRowsForPaths(TreePath[] path) {
//                throw new UnsupportedOperationException("Not supported yet.");
//            }
//    });
////        ShortPath shortPath = jdbcTemplate.query(q,
////                new RowMapper() {
////                    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
////                        ShortPath sp = new ShortPath();
////                        sp.setsId(rs.getInt("source"));
////                        sp.setsId(rs.getInt("target"));
////                        sp.setCost(rs.getFloat("cost"));
////                        return sp;
////                    }
////            @Override
////            public int[] getRowsForPaths(TreePath[] path) {
////                throw new UnsupportedOperationException("Not supported yet.");
////            }
////                });
//        return loginForm;
//    }
//}
